package com.youmingxing.activity.selfInfo;

import android.os.Bundle;

import com.youmingxing.R;
import com.youmingxing.activity.BaseActivity;
import com.youmingxing.adpter.PersonalInfoAuditionAdapter;
import com.youmingxing.bean.AuditionFromUser;
import com.youmingxing.data.GlobalDataManager;
import com.youmingxing.network.AllStarApi;
import com.youmingxing.network.BaseJsonHandler;
import com.youmingxing.view.RefreshListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by JHS on 2015/9/17
 * 我的全部海选
 */
public class MyAllAuditionsActivity extends BaseActivity {

    private PersonalInfoAuditionAdapter adapter;
    List<AuditionFromUser> dataList = new ArrayList<>();
    private RefreshListView listView;
    public static String EXTRAS_USER_ID = "user_id";
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseSetContentView(R.layout.activity_myallauditions);
        listView = (RefreshListView) findViewById(R.id.wrongquestion_ptr_lv);
        userId = getIntent().getExtras().getInt(EXTRAS_USER_ID);
        showDialog();
        if (userId == GlobalDataManager.userId) {
            AllStarApi.getMyAudition(-1, initHandler);
            setTopbarName(R.string.my_all_activities);
        } else {
            AllStarApi.getUserAudition(userId, -1, initHandler);
            setTopbarName(R.string.other_all_activities);
        }
    }

    public void onErrorClick() {
        if (userId == GlobalDataManager.userId) {
            AllStarApi.getMyAudition(-1, initHandler);
        } else {
            AllStarApi.getUserAudition(userId, -1, initHandler);
        }
    }


    BaseJsonHandler initHandler = new BaseJsonHandler(this) {
        @Override
        public void onSuccessWithData(JSONObject response) {
            hideDialog();
            showErrorPage(false);
            JSONArray data = response.optJSONArray("data");
            for (int i = 0; i < data.length(); i++) {
                AuditionFromUser auditionFromUser = new AuditionFromUser(data.optJSONObject(i));
                dataList.add(auditionFromUser);
            }
            listView.setFootVisible(false);
            adapter = new PersonalInfoAuditionAdapter(MyAllAuditionsActivity.this);
            adapter.setData(dataList);
            listView.setAdapter(adapter);
        }

        @Override
        public void onFail(int statusCode, String statusText) {
            super.onFail(statusCode, statusText);
            hideDialog();
            if (dataList == null || dataList.size() <= 0) {
                showErrorPage(true);
            }
            listView.endLoadMore();
            listView.setFootVisible(false);
        }
    };

}